import { StorageService } from './../../providers/StorageService';
import { UserService } from './../../providers/UserService';
import { Component } from '@angular/core';
import { IonicPage, NavController, NavParams, AlertController, ActionSheetController } from 'ionic-angular';

declare var $: any
declare var cordova: any;

/**
 * Generated class for the PayCashDepositByMoneyPage page.
 *
 * See https://ionicframework.com/docs/components/#navigation for more info on
 * Ionic pages and navigation.
 */

@IonicPage()
@Component({
  selector: 'page-pay-cash-deposit-by-money',
  templateUrl: 'pay-cash-deposit-by-money.html',
})
export class PayCashDepositByMoneyPage {
  
  private userinfo:any;

  /*支付宝支付需要的两个参数*/
  /*商品名称*/
  private service_name: any;

  /*订单号*/
  private outTradeNo:any;

  /*获取当前用户的订单号的 传入值*/
  param={
    shop_id:1,
    pay_type:1
  };

  /*使用代金劵支付的必须的参数*/
  payShopBondParam={
    user_id:null,
    order_sn:null,
    use_code:null,
  };

  constructor(public navCtrl: NavController, 
    public stc:StorageService,
    public alertCtrl:AlertController,
    private actionCtrl: ActionSheetController,
    public userService:UserService,
    public navParams: NavParams) {
  }

  ionViewDidLoad() {
    console.log('ionViewDidLoad PayCashDepositByMoneyPage');
    this.userinfo = this.stc.read('userinfo');
    this.param.shop_id = this.userinfo.shop_id;
    /*获取订单号*/
    this.orderSn(); 
    console.log(this.payShopBondParam);
    this.service_name = '店铺保证金';
  }


  /*使用现金付款*/
  async useCash(){
    this.gotoPlay();
  }

  gotoPlay(){
    this.actionCtrl.create({
      buttons: [
        {
          text: "支付宝支付",
          handler: () => {
            //let alipaystr = this.editOrderAPI(item);
            this.aliPay();
          }
        },
        // {
        //   text: "微信支付",
        //   handler: () => {
        //     this.aliPay2();
        //   }
        // },
        {
          text: "取消",
          role: 'cancel'
        }
      ]
    }).present();
  }

  /*支付宝支付接口*/
  aliPay() {
    console.log(333);
    /**
     * subject : 商品名称
     * outTradeNo ： 订单号
     * totalAmount： 金额
     * payType：付款的类型 1pc 2 app 3 手机网站
     */

    $.post('http://116.62.162.165:1228/api/alipay/uniformPayGateway', {
      subject: this.service_name,
      outTradeNo: this.outTradeNo,
      // totalAmount: 0.01,
      totalAmount: 1000,
      payType: 2,
    }, (data) => {
      //let payInfo=this.unescapeHTML(data);
      console.log(data);
      cordova.plugins.alipay.payment(data, (success) => {
      }, (error) => {
      });
    });
  }

  /*微信支付接口*/
  aliPay2() {
    $.post('http://192.168.1.99:1114/api/common/uniformPayer', {
      pay_type: 1,
      out_trade_no: 'SN23788',
      subject: 'laravel',
      total_amount: 0.01,
      alipay_type: 2
    }, (data) => {
      //let payInfo=this.unescapeHTML(data);
      console.log(data);
      data = this.unescapeHTML(data);
      cordova.plugins.alipay.payment(data, (success) => {
      }, (error) => {
      });
    });
  }

  unescapeHTML(a) {
    //let aNew = "" + a;
    let str = 'app_id=2016121504287738&biz_content=%7B%22product_code%22%3A%22QUICK_MSECURITY_PAY%22%2C%22subject%22%3A%22laravel%22%2C%22out_trade_no%22%3A%22SN23788%22%2C%22total_amount%22%3A120%2C%22body%22%3A%22%22%22seller_id%22%3A%2218827403620%40139.com%22%7D&charset=UTF-8&format=JSON&method=alipay.trade.app.pay&notify_url=http%3A%2F%2F192.168.1.99%3A1114%2FAdmin%2FAlipay%2Fnotifyurl&sign_type=RSA&timestamp=2018-02-09+09%3A52%3A06&version=1.0&sign=jVC%2FjWMqeVaKTOM4mntqV6hiSUC1Kn5SVHG981up1taiUGCHIQpHAz%2BqEz9%2FSjNRLhoMNEaYJGRf4E5fmkjzM6MBPQkGywFabZ%2Fr%2Bk0t6Iyrq9AKu6i1O0STSzTjjAaS9y%2BaQpq5fSJ190FLVfKAzstYuhVUXkCNw%2B%2FWCff4dt8%3D';
    //return aNew.replace(/</g, "<").replace(/>/g, ">").replace(/&/g, "&").replace(/"/g, '"').replace(/'/g, "'");
    return str;
  }


  /*获取订单信息*/
  async orderSn(){
    let res = await this.userService.generateBondOrderAPI(this.param);
    console.log(res);
    if(res.code == 0 || res.code== 1 ){
      console.log("生成了订单");

      /*将订单号保存起来*/
      this.outTradeNo = res.data.order_sn;
      
      if(res.data.coupon==0){
        console.log("没有代金劵");
      }else{
        console.log("生成的代金劵");
        this.payShopBondParam.use_code = res.data.coupon.use_code;
        this.payShopBondParam.order_sn = res.data.order_sn;  
        this.payShopBondParam.user_id = this.userinfo.user_id;
      }
    }else{
      console.log("网络错误");
      this.showAlert(res.msg);
    }
  }

  /*显示提示的功能*/
  showAlert(msg, title = '提示', callFunc = null) {
    let alert = this.alertCtrl.create({
      title: title,
      subTitle: msg,
      buttons: [
        {
          text: "确定",
          handler: data => {
            if (callFunc) {
              callFunc.apply(data);
            }
          }
        }]
    });
    alert.present();
  }

}
